package com.example.tourpal.user;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tourpal.R;
import com.example.tourpal.model.User;
import com.example.tourpal.model.UserDataManager;
import com.example.tourpal.model.UserDataManager.UserDataLishenter;
import com.example.tourpal.util.Tools;

public class FindPasswordActivity extends Activity {
	private static final String TAG = "chengongguo/FindPasswordActivity";
	private ImageView mBackTextView;
	private TextView mTitleTextView;
	private EditText mUserAccountEditText;
	private Button mSendEmailButton;
	private Tools mTools;
	private String mEnailAddress;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_forget_view);
		initCommonManager();
		initContentView();
	}

	private void initCommonManager() {
		// TODO Auto-generated method stub
		mTools = new Tools(this, TAG);
	}

	private void initContentView() {
		// TODO Auto-generated method stub
		mBackTextView = (ImageView) findViewById(R.id.back);
		mBackTextView.setOnClickListener(mListener);
		mTitleTextView = (TextView) findViewById(R.id.textview_user_title);
		mTitleTextView.setText(R.string.user_forget_title);
		mUserAccountEditText = (EditText) findViewById(R.id.edittext_forget_account);
		mSendEmailButton = (Button) findViewById(R.id.button_forget_send_email);
		mSendEmailButton.setOnClickListener(mListener);
	}

	OnClickListener mListener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.button_forget_send_email:
				sendEmail();
				break;

			case R.id.back:
				finish();
				break;
			}
		}
	};

	protected void sendEmail() {
		// TODO Auto-generated method stub
		if (isUserNameValid()) {
			User user = new User(1, mEnailAddress, null, 1, null);
			UserDataManager userDataManager = new UserDataManager(this);
			userDataManager.findPassword(user, new UserDataLishenter() {
				@Override
				public void requestUserData(User user) {
					mTools.showToast(getString(R.string.user_find_password_sucess));
				}
			});
		}
	}

	public boolean isUserNameValid() {
		mEnailAddress = mUserAccountEditText.getText().toString().trim();
		if (mEnailAddress.equals("")) {
			mTools.showToast(getString(R.string.user_account_empty));
			return false;
		}
		return true;
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.main_enter, R.anim.sub_exit);
	}
}